package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.dto.ZjNWarningHntDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjNWarningHntBo;
import com.ruoyi.zhgd.domain.vo.ZjNWarningHntVo;
import com.ruoyi.zhgd.domain.ZjNWarningHnt;
import com.ruoyi.zhgd.mapper.ZjNWarningHntMapper;
import com.ruoyi.zhgd.service.IZjNWarningHntService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 自建混凝土拌合站报警管理Service业务层处理
 *
 * @author zyb
 * @date 2023-11-01
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjNWarningHntServiceImpl implements IZjNWarningHntService {

    private final ZjNWarningHntMapper baseMapper;

    /**
     * 查询自建混凝土拌合站报警管理
     */
    @Override
    public ZjNWarningHntVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询自建混凝土拌合站报警管理列表
     */
    @Override
    public TableDataInfo<ZjNWarningHntVo> queryPageList(ZjNWarningHntBo bo, PageQuery pageQuery) {
        if(bo.getId() == null) {
            String deptId = LoginHelper.getDeptId().toString();
            bo.setId(deptId.equals("103") ? null : deptId);
        }
        Page<ZjNWarningHntVo> page = baseMapper.selectPageList(pageQuery.build(),bo);
        return TableDataInfo.build(page);

    }

    /**
     * 查询自建混凝土拌合站报警管理列表
     */
    @Override
    public List<ZjNWarningHntVo> queryList(ZjNWarningHntBo bo) {
        LambdaQueryWrapper<ZjNWarningHnt> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjNWarningHnt> buildQueryWrapper(ZjNWarningHntBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjNWarningHnt> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), ZjNWarningHnt::getType, bo.getType());
        lqw.like(bo.getInsterTime() != null, ZjNWarningHnt::getInsterTime, bo.getInsterTime());
        lqw.eq(StringUtils.isNotBlank(bo.getMixingStation()), ZjNWarningHnt::getMixingStation, bo.getMixingStation());
        lqw.eq(StringUtils.isNotBlank(bo.getMessage()), ZjNWarningHnt::getMessage, bo.getMessage());
        lqw.eq(StringUtils.isNotBlank(bo.getStationCode()), ZjNWarningHnt::getStationCode, bo.getStationCode());
        lqw.eq(StringUtils.isNotBlank(bo.getWarningGrade()), ZjNWarningHnt::getWarningGrade, bo.getWarningGrade());
        lqw.eq(StringUtils.isNotBlank(bo.getIdentifier()),ZjNWarningHnt::getStationCode,bo.getIdentifier());
        lqw.orderByDesc(ZjNWarningHnt::getInsterTime);
        return lqw;
    }

    /**
     * 新增自建混凝土拌合站报警管理
     */
    @Override
    public Boolean insertByBo(ZjNWarningHntBo bo) {
        ZjNWarningHnt add = BeanUtil.toBean(bo, ZjNWarningHnt.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改自建混凝土拌合站报警管理
     */
    @Override
    public Boolean updateByBo(ZjNWarningHntBo bo) {
        ZjNWarningHnt update = BeanUtil.toBean(bo, ZjNWarningHnt.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjNWarningHnt entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除自建混凝土拌合站报警管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<ZjNWarningHntDto> selectDayStatistics(Map<String, Object> params) {
        return baseMapper.selectDayStatistics(params);
    }
}
